package com.aidong.web.controller.box;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BoxCodeDao;
import com.aidong.model.wills.BoxCode;
import com.aidong.model.wills.BoxCodePK;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"boxCode"})
public class BoxCodeController extends BaseController {
    @Resource
    BoxCodeDao boxCodeDao;

    public BoxCodeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("box/code/list")).addObject("useType", this.getTypeList("boxUseType")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "useType",required = false) String useType, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("shopCode", shopCode);
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        cd.eq("useType", useType);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.boxCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("box/code/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        BoxCode code = new BoxCode();
        code.setShopCode(this.getLoginShopCode());
        code.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("box/code/edit");
        mv.addObject("code", code);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("useType", this.getTypeList("boxUseType"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("code") BoxCode code, @RequestParam(value = "code2",required = false) String code2) {
        if(this.chkNull(code.getShopCode())) {
            return this.getResult(false, "请选择门店");
        } else if(this.chkNull(code.getCode())) {
            return this.getResult(false, "请输入箱号");
        } else {
            if(code.getChName() == null) {
                code.setChName("");
            }

            if(this.chkNull(code.getStatus())) {
                code.setStatus(Integer.valueOf(1));
            }

            if(this.boxCodeDao.exists(new BoxCodePK(code.getShopCode(), code.getCode()))) {
                return this.getResult(false, "箱号已存在");
            } else {
                code.setCreateUser(this.getLoginUserCode());
                code.setCreateTime(new Date());
                code.setUpdateTime(new Date());
                ArrayList codeList = new ArrayList();
                String c;
                if(!this.chkNull(code2)) {
                    String e = code.getCode();
                    c = code2;
                    if(e.length() != code2.length()) {
                        return this.getResult(false, "开始和结束箱号长度必须一致");
                    }

                    if(this.boxCodeDao.chkCodeExists(code.getShopCode(), e, code2)) {
                        return this.getResult(false, "箱号已经存在");
                    }

                    int nn = e.length();
                    int n = 0;

                    label76:
                    while(true) {
                        if(e.charAt(n) != c.charAt(n)) {
                            Integer n1 = this.parseInt(e.substring(n));
                            Integer n2 = this.parseInt(c.substring(n));
                            if(n1 == null || n2 == null) {
                                return this.getResult(false, "序号部分必须是数字");
                            }

                            if(n2.intValue() - n1.intValue() > 1000) {
                                return this.getResult(false, "一次生成的箱号太多");
                            }

                            String st = e.substring(0, n);
                            n = nn - n;
                            int i = n1.intValue();

                            while(true) {
                                if(i > n2.intValue()) {
                                    break label76;
                                }

                                String t = "00000000" + i;
                                codeList.add(st + t.substring(t.length() - n));
                                ++i;
                            }
                        }

                        ++n;
                    }
                }

                try {
                    if(codeList.size() > 1) {
                        Iterator var14 = codeList.iterator();

                        while(var14.hasNext()) {
                            c = (String)var14.next();
                            code.setCode(c);
                            this.boxCodeDao.add(code);
                        }

                        this.addUserLog("新增更衣箱", "门店:" + code.getShopCode() + ",编号:" + code.getCode() + "-" + code2 + ",名称:" + code.getChName());
                    } else {
                        this.boxCodeDao.add(code);
                        this.addUserLog("新增更衣箱", "门店:" + code.getShopCode() + ",编号:" + code.getCode() + ",名称:" + code.getChName());
                    }
                } catch (Exception var13) {
                    this.log.error("add boxcode", var13);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@ModelAttribute("pk") BoxCodePK pk) {
        BoxCode code = (BoxCode)this.boxCodeDao.getById(pk);
        if(code == null) {
            return this.showAlert("没有这个更衣箱");
        } else {
            ModelAndView mv = new ModelAndView("box/code/edit");
            mv.addObject("code", code);
            mv.addObject("shopName", this.getTypeList("shopName"));
            mv.addObject("useType", this.getTypeList("boxUseType"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("code") BoxCode code) {
        if(this.chkNull(code.getShopCode())) {
            return this.getResult(false, "没有这个更衣箱");
        } else if(this.chkNull(code.getCode())) {
            return this.getResult(false, "没有这个更衣箱");
        } else {
            if(code.getChName() == null) {
                code.setChName("");
            }

            if(this.chkNull(code.getStatus())) {
                code.setStatus(Integer.valueOf(1));
            }

            BoxCode code1 = this.boxCodeDao.getByCode(code.getShopCode(), code.getCode());
            if(code1 == null) {
                return this.getResult(false, "没有这个更衣箱");
            } else if(code.getStatus().intValue() == 2 && !this.boxCodeDao.chkBoxFree(code.getShopCode(), code.getCode())) {
                return this.getResult(false, "更衣箱租用中,不能停用");
            } else {
                code1.setChName(code.getChName());
                code1.setDeposit(code.getDeposit());
                code1.setYearFee(code.getYearFee());
                code1.setMonthFee(code.getMonthFee());
                code1.setUseType(code.getUseType());
                code1.setNote(code.getNote());
                code1.setStatus(code.getStatus());
                code1.setUpdateTime(new Date());

                try {
                    this.boxCodeDao.update(code1);
                    this.addUserLog("修改更衣箱", "门店:" + code.getShopCode() + ",编号:" + code1.getCode() + ",名称:" + code1.getChName());
                } catch (Exception var4) {
                    this.log.error("edit boxcode", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@ModelAttribute("pk") BoxCodePK pk) {
        BoxCode code = (BoxCode)this.boxCodeDao.getById(pk);
        if(code == null) {
            return this.getResult(false, "没有这个更衣箱");
        } else if(!this.boxCodeDao.chkBoxFree(code.getShopCode(), code.getCode())) {
            return this.getResult(false, "更衣箱租用中,不能删除");
        } else {
            try {
                this.boxCodeDao.updateStatus(code, 0);
                this.addUserLog("删除更衣箱", "门店:" + code.getShopCode() + ",编号:" + code.getCode() + ",名称:" + code.getChName());
            } catch (Exception var4) {
                this.log.error("del boxcode", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}